package com.ist.neartweat;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;

import com.ist.neartweat.messages.BroadCastMessage;
import com.ist.neartweat.messages.ByeMessage;
import com.ist.neartweat.messages.Message;
import com.ist.neartweat.messages.PrivateMessage;

public class NearTweatServer{
	private Hashtable<String,Socket> clientHandles =  new Hashtable<String,Socket>();
	
	public static void main(String args[]){
		try {
			ServerSocket server = new ServerSocket(9876);
			NearTweatServer tweatServer =  new NearTweatServer();
			System.out.println("Succefully created the server");
			Socket clientSocket;
			//Accept Connections
			while(true){
				clientSocket = server.accept();
				System.out.println("Server accepted the client");
				new ClientHandlerThread(tweatServer,clientSocket).start();
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Failed to create the server at port 9876");
			e.printStackTrace();
		}
	}

	public Boolean addClient(String clientId, Socket clientSocket) {
		if(clientHandles.containsKey(clientId)){
			return false;
		}
		else{
			clientHandles.put(clientId, clientSocket);
			return true;
		}
	}

	public void handleMessage(Message msg) {
		if(msg instanceof PrivateMessage){
			System.out.println("Received Private Message");
		}
		else if(msg instanceof BroadCastMessage){
			
		}
		else if(msg instanceof ByeMessage){
			
		}
		
	}
}